from app.models import *

from app.url import *
import json


# 获取轮播图
def get_banner():
    banner_list = Banner.objects.all()
    return banner_list


# 发布动态
def insert_blog(data):
    try:
        UserBlog.objects.create(data)
        return True
    except Exception as e:
        print(e)
        return False


# 获取所有动态
def get_blog(**kwargs):
    data = UserBlog.objects.all()
    list1 = []
    for i in data:
        list1.append({
            "id": i.id,
            "content": i.content,
            "image": i.image_paths,
            "username": i.user.userinfo.nickname,
            "avatar": i.user.userinfo.avatar,
            "gender": i.user.userinfo.gender,
            "my_city": i.user.userinfo.my_city,
            "height": i.user.userinfo.height,
            "education": i.user.userinfo.education,
            'age': i.user.userinfo.age,
            "user_id": i.user.id,
            "like_num": i.likes_num,
        })
    print(list1)
    return list1


# 点赞
def like_ctl(params):
    print(params)
    try:
        if params is not None:
            Likes.objects.create(user_id=params['user_id'], blog_id=params['blog_id'], is_like=1)
            return True
        else:
            return False
    except Exception as e:
        print(f"出错啦！{e}")


def is_liked(params):
    try:
        if params is not None:
            data = Likes.objects.filter(user_id=params['user_id'], blog_id=params['blog_id'])
            print(data, "点赞数据")
        else:
            return False
    except Exception as e:
        print(e)


# 添加礼物
def commit_gift(data):
    try:
        if data is not None:
            Gifts.objects.create(id=uuid.uuid4(), name=data['name'], image=data['image'], price=data['price'])
            return True
    except Exception as e:
        print(e)
        return False


# 送礼物
def send_gift(params):
    try:
        GiftTransaction.objects.create(user_id=params['id'], blog_id=params['blog'], gift_id=params['gifts'],
                                       send_user_id=params['send_user'])

        return consumer_coins(
            {
                "user_id": params['id'],
                "msg": "赠送礼物" + params['gift_name'],
                "limit": 10
            }
        )
    except Exception as e:
        print(f"出错啦！{e}")
        return False


# 获取所有礼物
def get_all_gifts():
    try:
        data = Gifts.objects.all()
        gifts = []
        for i in data:
            gifts.append({
                "id": i.id,
                "name": i.name,
                "image": develop_url + i.image,
                "price": i.price,
                "video": develop_url + i.video,
            })
        return gifts
    except Exception as e:
        print(e)


# 关注用户
def insert_attention_user(id):
    print(id, "id")
    try:
        UserAttentions.objects.create(user_id=id)
        return True
    except Exception as e:
        print(e)
        return False


# 获取我喜欢的数量
def get_all_attention(id):
    try:
        count = UserAttentions.objects.filter(user_id=id).count()
        return count
    except Exception as e:
        print(e)


# 花费金币
def consumer_coins(params):
    try:
        Details.objects.create(
            user_id=params['user_id'],
            source=params['msg'],
            amount=params['limit']
        )
        try:
            # 获取当前用户的金币
            num = Gold.objects.get(id=params['user_id']).money
            Gold.objects.filter(id=params['user_id']).update(money=num - params['limit'])
            return True
        except Exception as e:
            print(e)


    except Exception as e:
        print(e)
        return False
